#include "OtherMoneyDlg.h"
#include "resource.h"
#include "AccountBussiness.h"
#include "PrintDlg.h"
#include "ValidateCardDlg.h"
#include "MenuDlg.h"
#include "common.h"

LRESULT CALLBACK OtherDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
	OtherMoneyDlg *Obj;
	if (message==WM_INITDIALOG)
	{	
		Obj=(OtherMoneyDlg*)lParam;
		SetWindowLong(hWnd,GWL_USERDATA,(LONG)Obj);
	}
	Obj=(OtherMoneyDlg*)GetWindowLong(hWnd,GWL_USERDATA);
	if (Obj)
		return Obj->DialogProc(hWnd, message, wParam, lParam);
	return 0;
}

OtherMoneyDlg::OtherMoneyDlg(HINSTANCE hInstance, HWND hParent)
{
	DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG_OTHER), hParent, (DLGPROC)OtherDlgProc, (LPARAM)this);
}

OtherMoneyDlg::~OtherMoneyDlg(void)
{
	EndDialog(m_hDlg,0);
}

LRESULT OtherMoneyDlg::DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    long lAmount;
    bool bFlag;
    AccountBussiness objAccBussiness;
    char *sAccountID = g_Card.GetAccountID();
    HWND hEditAmountWithdraw;

	switch (message) 
	{
	case WM_INITDIALOG:
		{	   
			m_hDlg = hWnd;  
            hEditAmountWithdraw = GetDlgItem(m_hDlg, IDC_EDIT_AMOUNT_WITHDRAW);
            SetFocus(hEditAmountWithdraw);
			return FALSE;
		}		
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{	
		case IDOK:			
            hEditAmountWithdraw = GetDlgItem(m_hDlg, IDC_EDIT_AMOUNT_WITHDRAW);
            char sBuffer[100];
            GetWindowText(hEditAmountWithdraw, sBuffer, 100);
            lAmount = atoi(sBuffer);
            if (lAmount%10000 == 0)
            {
                bFlag = objAccBussiness.CheckWithdraw(sAccountID, lAmount);

                if (bFlag) 
                {
                    int MsgBoxID=MessageBox(m_hDlg, PRINT_RECEIPT_TEXT, PRINT_RECEIPT_CAPTION, MB_YESNO|MB_ICONQUESTION);
                    if (MsgBoxID == IDYES)
                    {
                        EndDialog(m_hDlg, 0);
                        CPrintDlg objPrintDlg(hGlobalInst, m_hDlg, lAmount, 1);							
                        break;
                    }
                    else
                    {
                        EndDialog(m_hDlg, 0);
                        CValidateCardDlg objValidateCard(hGlobalInst,m_hDlg);						
                        break;
                    }
                }
                else 
                {
                    int MsgBoxID=MessageBox(m_hDlg,CONTINOUS_TRANSACTION_TEXT, CONTINOUS_TRANSACTION_CAPTION,MB_YESNO|MB_ICONQUESTION);

                    if (MsgBoxID == IDYES)
                    {
                        EndDialog(m_hDlg, 0);
                        MenuDlg objMenuDlg(hGlobalInst,m_hDlg);						
                        break;
                    }
                    else
                    {
                        EndDialog(m_hDlg, 0);
                        CValidateCardDlg objValidateCard(hGlobalInst,m_hDlg);						
                        break;
                    }
                }
            }
            else
            {
                MessageBox(hWnd, ERROR_WITHDRAW_OTHER, ERROR_WITHDRAW_CAPTION, MB_ICONERROR);
                int MsgBoxID=MessageBox(m_hDlg,CONTINOUS_TRANSACTION_TEXT,CONTINOUS_TRANSACTION_CAPTION,MB_YESNO|MB_ICONQUESTION);
                if (MsgBoxID == IDYES)
                {
                    EndDialog(m_hDlg,0);
                    MenuDlg objMenuDlg(hGlobalInst,m_hDlg);						
                    break;
                }
                else
                {
                    EndDialog(m_hDlg,0);
                    CValidateCardDlg objValidateCard(hGlobalInst,m_hDlg);						
                    break;
                }
            }
            return TRUE;
		case IDCANCEL:
			{
				EndDialog(hWnd, 0);
                CValidateCardDlg objValidateCard(hGlobalInst,m_hDlg);
				return TRUE;
			}		
		}
        break;
	default:
		return FALSE;
	}
	return TRUE;
}
